<?php
class QSource_System_Controller_Action extends Zend_Controller_Action {
	var $request;
	public function loadTemplate($template){	
	  $templateConfig = BASE_PATH . '/public/templates/'.$template.'/template.ini';          
      $config = new Zend_Config_Ini($templateConfig, 'template');          
      $urlTemplate = $this->_request->getBaseUrl() . $config->url;
      $dirCss = $urlTemplate .  $config->dir->css;
      $dirJs = $urlTemplate . $config->dir->js;
      $dirImg = $urlTemplate . $config->dir->img;      
      $this->view->dirImg = $dirImg;
      $this->view->headTitle($config->title,true);
      foreach($config->file->css as $key => $item){         
         $this->view->headLink()->appendStylesheet($dirCss . $item);         
      }
      //Set lang
      $lang = $this->_request->getParam('lang');
      $this->view->lang = $lang;
      /*---END---*/
      foreach($config->file->js as $key => $item){         
         $this->view->headScript()->appendFile($dirJs . $item,'text/javascript');   
      }
  		//Set translation since QSource v1.0
  		$this->view->translate = new QSource_System_Language_Translate();
      	//End set translation
        $layoutPath = $config->folder;
        $option = array ('layout' => 'index', 
                   'layoutPath' => $layoutPath );
      	Zend_Layout::startMvc ($option);    
     	//Set các request cho controllers - Thông tin đăng nhập, request controller, action, ...
     	$auth = Zend_Auth::getInstance();
     	if ($auth->hasIdentity()) {
			$info = $auth->getIdentity();
			$this->view->user_info = $info;
			$this->user = $info;
     	}
		$request['module'] = $this->_request->getModuleName();
		$request['action'] = $this->_request->getActionName();
		$request['controller'] = $this->_request->getControllerName();
		$this->view->request = $request;  
	}
	public function preDispatch() {
		if($this->_request->getModuleName()=='administrator')
		{
			$auth = Zend_Auth::getInstance();
			if (!$auth->hasIdentity()) {
				
					if($this->_request->getControllerName()!='login')
						$this->_redirect('/administrator/login');
			}
		}
		$model = new Model_SiteInfo_SiteInfo();
		$info = $model->getItemId(1);
		if($info['offline']==1)
		{
		    if($this->_request->getModuleName()=='default')
		    {
		    	$this->_request->setControllerName('index');
		    	$this->_request->setActionName('siteoffline');
		    }
		}
	}
}